package service

import (
	"errors"

	"gitee.com/cyanbleeze/go-image-management/internal/model"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
)

type TogglePublicRequest struct {
	ID     uint `json:"id" binding:"required,min=1"`
	Public bool `json:"public,omitempty"`
}

func (svc *Service) GetPublicPictureList(pager *app.Pager) ([]*model.Picture, int, error) {
	detailCount, err := svc.dao.CountAllPublicList()
	if err != nil {
		return nil, 0, err
	}
	detailList, err := svc.dao.GetPublicPictureList(pager.Page, pager.PageSize)
	if err != nil {
		return nil, 0, err
	}
	return detailList, detailCount, nil
}

func (svc *Service) TogglePublic(params *TogglePublicRequest) error {
	userID, exist := svc.ctx.Get("userID")
	if !exist {
		return errors.New("登录用户信息不存在")
	}
	_, err := svc.dao.GetPictureByUID(
		params.ID,
		userID.(uint),
	)
	if err != nil {
		return errors.New("图片不存在")
	}
	return svc.dao.UpdatePicturePublic(
		params.ID,
		params.Public,
	)
}
